package mn.usi.spendcontrol.client;

import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.RootPanel;

public class HistoryListener implements ValueChangeHandler<String>{
	public void onValueChange(ValueChangeEvent<String> event)
	{
		String historyToken = event.getValue();
		RootPanel.get("body").clear();
		RootPanel.get("nav").clear();
		RootPanel.get("welcome").clear();
		RootPanel.get("tree").clear();
		RootPanel.get("tree1").clear();
		
		try{
			Mainpage mainpage = new Mainpage();
			if(historyToken.equals("mainpage"))
			{
				
		
				RootPanel.get("tree").add(mainpage);
				RootPanel.get("welcome").add(new HTML("<span>Тавтай морил! <a href=\"#\">Хэрэглэгч</a>, <a href=\"#\">Гарах</a></span>"));
				
			}
			else if(historyToken.equals("login"))
			{
				Login loginPage = new Login();
				RootPanel.get("body").add(loginPage);
			}
			else if(historyToken.equals("budget"))
			{
				
				Action obj = new Action("budget");
				RootPanel.get("tree1").clear();
				RootPanel.get("tree1").add(obj);
				
				Tusuv tusuvPage = new Tusuv();
				RootPanel.get("body").clear();
				RootPanel.get("body").add(tusuvPage);

				RootPanel.get("tree").clear();
				RootPanel.get("tree").add(mainpage);
				RootPanel.get("welcome").add(new HTML("<span>Тавтай морил! <a href=\"#\">Хэрэглэгч</a>, <a href=\"#\">Гарах</a></span>"));
			}
			else if(historyToken.equals("catalog"))
			{
				Action obj = new Action("catalog");
		
				RootPanel.get("tree1").add(obj);
				
				Catalog catalog = new Catalog();
	
				RootPanel.get("body").add(catalog);
				
		
				RootPanel.get("tree").add(mainpage);
				RootPanel.get("welcome").add(new HTML("<span>Тавтай морил! <a href=\"#\">Хэрэглэгч</a>, <a href=\"#\">Гарах</a></span>"));
			}
			else if(historyToken.equals("partner"))
			{
				Partner partnerPage = new Partner();
				RootPanel.get("body").add(partnerPage);
				
			
				RootPanel.get("tree").add(mainpage);
				RootPanel.get("welcome").add(new HTML("<span>Тавтай морил! <a href=\"#\">Хэрэглэгч</a>, <a href=\"#\">Гарах</a></span>"));
			}
			else if(historyToken.equals("contract"))
			{
				Partner partnerPage = new Partner();
				RootPanel.get("body").add(partnerPage);
	
				RootPanel.get("tree").add(mainpage);
				RootPanel.get("welcome").add(new HTML("<span>Тавтай морил! <a href=\"#\">Хэрэглэгч</a>, <a href=\"#\">Гарах</a></span>"));
				Partner.partnerContractPanel.selectTab(1);	
			}
			
		}catch(IndexOutOfBoundsException e)
		{
			History.newItem("login");
		}
	}
}
